#!/usr/bin/env python3
# coding:utf-8
"""统计文件数量
"""

# end_pymotw_header
import os

count = 0
ingore_file = ["images", "count.py", "README.md", "SUMMARY.md", "redis-3.0.0.gem", "_book", "gen_summary.py" ]
pwd = os.getcwd()
# pwd = "D:/zhuliangcai/markdown/gitbook"

# 打开一个文件
fo = open(pwd + "/SUMMARY.md", "w", encoding="utf8")


def write(string):
    fo.write(string + "\n")


write("""
# Summary

* [简介](README.md)

-----
""")


def list_files(path):
    global count
    # print(path)
    files = os.listdir(path)
    num = 0
    for name in files:
        if name in ingore_file:
            continue
        file = path + "/" + name
        # print(file)
        if os.path.isdir(file):
            write("* [%s]" % name)
            list_files(file)
        else:
            num += 1
            md_name = file.replace(pwd, "")
            title = md_name.replace(".md", "")[md_name.rindex("/") + 1:]
            write("    * [%d.%s](%s)" % (num, title, md_name[md_name.index("/") + 1:]))
            count = count + 1
    return count


# print(pwd) # F:\git\zhuliangcai.github.io\_posts

file_counts = list_files(pwd)

print(file_counts)

# 关闭打开的文件
fo.close()
